package com.jinlia.show.modules.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinlia.show.common.core.utils.UserInfoUtils;
import com.jinlia.show.modules.base.model.po.GoodsPO;
import com.jinlia.show.modules.base.model.vo.SysMenuVO;
import com.jinlia.show.modules.base.system.entity.Goods;
import com.jinlia.show.modules.base.system.entity.SysMenu;
import com.jinlia.show.modules.base.system.entity.SysRole;
import com.jinlia.show.modules.base.system.service.SysMenuMapperService;
import com.jinlia.show.modules.base.system.service.SysRoleMapperService;
import com.jinlia.show.modules.base.wxapp.entity.User;
import com.jinlia.show.modules.system.constant.SystemConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SysMenuService extends SysMenuMapperService {


    @Autowired
    SysRoleMapperService sysRoleMapperService;

    /**
     * 获取当前用户的菜单树
     *
     * @return
     */
    public List<SysMenuVO> getCurrentMenuTree() {
        //获取当前用户角色
        List<SysRole> rolesByUserId = sysRoleMapperService.getRolesByUserId(UserInfoUtils.getUserId());
        //获取角色的菜单
        List<SysMenuVO> menuTreeByRoleId = this.getMenuTreeByRoleId(rolesByUserId.stream().map(SysRole::getId).toArray(Long[]::new));
        return menuTreeByRoleId;
    }

    public IPage<SysMenu> getMenuButtonPage(Long menuId) {
        Page<SysMenu> page = new Page<>(1, 100);
        //构造查询参数
        LambdaQueryWrapper<SysMenu> sysMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysMenuLambdaQueryWrapper.eq(SysMenu::getParentId, menuId);
        sysMenuLambdaQueryWrapper.eq(SysMenu::getMenuType, "BUTTON");
        return this.page(page, sysMenuLambdaQueryWrapper);
    }
}
